import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:guochaoapp/common/constant/order_constant.dart';
import 'package:guochaoapp/common/constant/to_order_detail_source.dart';
import 'package:guochaoapp/common/widges/appbar.dart';
import 'package:guochaoapp/controller/mine/mine_controller.dart';
import 'package:guochaoapp/controller/shopping/order_detail_controller.dart';
import 'package:guochaoapp/controller/shopping/shopping_controller.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';
import 'package:guochaoapp/routes/app_pages.dart';

import '../../r.dart';

class GCOrderDetailPage extends StatelessWidget {
  GCOrderDetailController c;

  @override
  Widget build(BuildContext context) {
    c = Get.put(GCOrderDetailController());
    String mallOrderNo = Get.arguments[0];
    String mallOrderStatus  = Get.arguments[1];
    String source = Get.arguments[2];
    bool isFinish = false;
    if (mallOrderStatus == OrderConstant.statusFinish)
      isFinish = true;
    print(isFinish);
    return Scaffold(
        appBar: GCAppBar(
            titleText: "订单详情",
            backTap: () {
              if(source == ToOrderDetailSource.mineTo) {
                Get.back();
                GCMineController mineController = Get.find();
                mineController.requestOrderList(false, isFinish);
              } else {
                Get.until((route) => Get.currentRoute == Routes.INITIAL);
                GCShoppingController shopping_c = Get.find();
                shopping_c.requestInitData();
              }
            }),
        body: FutureBuilder(
            future: c.requestInitData(mallOrderNo),
            builder: (context, AsyncSnapshot snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                return Obx(() => _initSubviews());
              }
              return Text('Loading...');
            }));
  }

  _initSubviews() {
    return Container(
        child: Column(children: [
      Container(
        height: ScreenUtil().screenHeight - 162,
        child: ListView(
          children: [
            Column(
              children: [
                _getTopView(),
                _getTransferView(),
                _getSettleView(),
                _getOrderInfoView(),
              ],
            ),
          ],
        ),
      ),
      Expanded(child: SizedBox()),
      _getButtonView()
    ]));
  }

  _getTopView() {
    return Container(
      child: Stack(children: [
        Container(
          height: 159 - ScreenUtil().statusBarHeight,
          width: ScreenUtil().screenWidth,
          color: Theme.of(Get.context).primaryColor,
          child: Column(
            children: [
              Container(
                  margin: EdgeInsets.only(top: 20),
                  height: 60,
                  child: _getOrderStatusImage())
            ],
          ),
        ),
        Container(
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(4.0),
          ),
          margin: EdgeInsets.only(
              left: 8,
              right: 8,
              top: 100),
          padding: EdgeInsets.only(bottom: 10, right: 10),
          child: Column(
            children: [
              SizedBox(height: 10),
              Row(
                children: [
                  SizedBox(width: 21),
                  GestureDetector(
                    onTap: () {
                      Get.toNamed(Routes.PAYMENT_SUCCESS);
                    },
                    child: Image.asset(R.assetsIconSettleAddress,
                        height: 18,
                        width: 18),
                  ),
                  SizedBox(width: 8),
                  Text("收货地址",
                      style: TextStyle(
                          fontSize: 12,
                          color: Color(0xFF333333))),
                ],
              ),
              Row(
                children: [
                  SizedBox(width: 47),
                  Text(
                      "联系人: ${c.orderDetail.value.receiverName}    ${c.orderDetail.value.receiverPhone}",
                      style: TextStyle(fontSize: 12, color: Color(0xFF333333))),
                ],
              ),
              Row(
                children: [
                  SizedBox(width: 47),
                  Expanded(
                      child: Text("地址: ${c.orderDetail.value.receiverAddr}",
                          style: TextStyle(
                              fontSize: 12, color: Color(0xFF333333)))),
                ],
              ),
            ],
          ),
        )
      ]),
    );
  }

  _getSettleView() {
    return Container(
        color: Colors.white,
        width: ScreenUtil().screenWidth,
        margin: EdgeInsets.only(
            left: 8,
            right: 8,
            top: 4),
        padding: EdgeInsets.only(
            left: 20,
            right: 18,
            top: 12),
        child: Column(
          children: [
            Row(
              children: [
                Image.asset(R.assetsIconSettleMall,
                    height: 16,
                    width: 16),
                SizedBox(width: 5),
                Text("果巢商城",
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.bold)),
                Expanded(child: SizedBox()),
                Container(
                  width: 65,
                  height: 26,
                  child: OutlineButton.icon(
                    icon: Image.asset(R.assetsIconSettleList,
                        height: 15,
                        width: 15),
                    label: Text("清单"),
                    padding: EdgeInsets.zero,
                    color: Colors.white,
                    textColor: Colors.black54,
                    borderSide: BorderSide(color: Color(0xFF40A8B3)),
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(Radius.circular(4))),
                    onPressed: () {
                      c.jumpToList(c.orderDetail.value.itemList);
                    },
                  ),
                )
              ],
            ),
            SizedBox(height: 7),
            Divider(
              height: 1,
              color: Color(0xFFF0F0F0),
            ),
            Container(
              height: 33.0 * c.orderDetail.value.itemList.length,
              child: ListView.builder(
                  itemCount: c.orderDetail.value.itemList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getListView(index);
                  }),
            ),
            SizedBox(height: 5),
            Row(
              children: [
                Text(
                  "总重量",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "${c.getWeightTotal()}",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
              ],
            ),
            SizedBox(height: 5),
            Row(
              children: [
                Text(
                  "商品总额",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "￥${c.orderDetail.value.priceTotal}",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
              ],
            ),
            SizedBox(height: 5),
            Row(
              children: [
                Text(
                  "运费",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "+${c.orderDetail.value.freightTotal}",
                  style: TextStyle(
                      fontSize: 12, color: Colors.red),
                ),
              ],
            ),
            SizedBox(height: 5),
            Row(
              children: [
                Text(
                  "余额",
                  style: TextStyle(
                    fontSize: 12,
                  ),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "-${c.orderDetail.value.balanceDeductionTotal}",
                  style: TextStyle(
                      fontSize: 12, color: Colors.green),
                ),
              ],
            ),
            SizedBox(height: 5),
            Row(
              children: [
                Text(
                  "实付金额",
                  style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.bold),
                ),
                Expanded(child: SizedBox()),
                Text(
                  "￥${c.orderDetail.value.paymentPriceTotal}",
                  style: TextStyle(
                      fontSize: 12,
                      fontWeight: FontWeight.bold),
                ),
              ],
            ),
            SizedBox(height: 15)
          ],
        ));
  }

  _getListView(int index) {
    CartSettleDataItemList data = c.orderDetail.value.itemList[index];
    return Container(
      padding: EdgeInsets.only(top: 3),
      height: 33,
      child: Column(
        children: [
          Row(
            children: [
              Container(
                height: 26,
                child: FlatButton(
                  child: Row(
                    children: [
                      Image.network(data.categoryImg,
                          height: 15,
                          width: 15),
                      SizedBox(width: 4),
                      Text("${data.categoryName}")
                    ],
                  ),
                  color: Color(0xFF40A8B3),
                  textColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(4))),
                  onPressed: () {},
                ),
              ),
              SizedBox(width: 3),
              Text("(共${data.itemNum}件)",
                  style: TextStyle(fontSize: 12)),
              Expanded(child: SizedBox()),
              Opacity(
                  opacity: 0.5,
                  child: Text("￥${data.itemPrice}",
                      style: TextStyle(fontSize: 12))),
            ],
          ),
          SizedBox(height: 3),
          Divider(
            height: 1,
            color: Color(0xFFF0F0F0),
          ),
        ],
      ),
    );
  }

  _getOrderInfoView() {
    return Container(
      height: 50,
      margin: EdgeInsets.only(
          top: 8,
          left: 28,
          right: 28),
      child: Column(
        children: [
          Row(
            children: [
              Opacity(
                  opacity: 0.5,
                  child: Text("订单信息",
                      style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.bold))),
              Expanded(child: SizedBox()),
              Opacity(
                  opacity: 0.5,
                  child: Text("${c.orderDetail.value.createAt}",
                      style: TextStyle(fontSize: 12)))
            ],
          ),
          SizedBox(height: 5),
          Row(
            children: [
              Opacity(
                  opacity: 0.5,
                  child: Text("订单号: ${c.orderDetail.value.mallOrderNo}",
                      style: TextStyle(fontSize: 12)))
            ],
          ),
        ],
      ),
    );
  }

  _getButtonView() {
    if (c.orderDetail.value.mallOrderStatus == OrderConstant.statusInit &&
        c.orderDetail.value.receiptCardDTO.bankName == null) {
      return Container(
        margin: EdgeInsets.only(
            left: 8,
            right: 8),
        child: Row(
          children: [
            Container(
              width: 172,
              child: OutlineButton(
                child: Text("取消订单"),
                padding: EdgeInsets.zero,
                color: Colors.white,
                textColor: Colors.red,
                borderSide: BorderSide(color: Colors.red),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                onPressed: () {
                  c.cancelOrder();
                },
              ),
            ),
            SizedBox(width: 15),
            Container(
              width: 172,
              child: FlatButton(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text("立即支付"),
                    Text("剩${c.orderDetail.value.expireHours}小时自动关闭",
                        style: TextStyle(
                            fontSize: 10,
                            color: Colors.red))
                  ],
                ),
                padding: EdgeInsets.zero,
                color: Color(0xFF40A8B3),
                textColor: Colors.white,
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4))),
                onPressed: () {
                  c.jumpToPayment();
                },
              ),
            ),
          ],
        ),
      );
    }
    if (c.orderDetail.value.mallOrderStatus == OrderConstant.statusInit &&
        c.orderDetail.value.receiptCardDTO.bankName != null) {
      return Container(
        color: Colors.white,
        height: 39,
        margin: EdgeInsets.only(
            left: 8,
            right: 8),
        child: Align(
          child:
              Text("请尽快将货款转入对应账户如已转请忽略", style: TextStyle(color: Colors.red)),
        ),
      );
    } else if (c.orderDetail.value.mallOrderStatus ==
            OrderConstant.statusStocking ||
        c.orderDetail.value.mallOrderStatus == OrderConstant.statusTransport) {
      return Container(
          // color: Colors.red,
          margin: EdgeInsets.only(
              bottom: 20,
              left: 8,
              right: 8));
    } else if (c.orderDetail.value.mallOrderStatus ==
        OrderConstant.statusFinish) {
      if (!c.orderDetail.value.hasReturn && c.orderDetail.value.hasInvoiced) {
        return Container(
            width: 359,
            margin: EdgeInsets.only(
                bottom: 20,
                left: 8,
                right: 8),
            child: FlatButton(
              child: Text("查看发票"),
              padding: EdgeInsets.zero,
              color: Color(0xFF40A8B3),
              textColor: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(4))),
              onPressed: () {
                c.jumpToInvoice();
              },
            ));
      } else {
        return Container(
          // color: Colors.red,
          margin: EdgeInsets.only(
              bottom: 20,
              left: ScreenUtil().setSp(10),
              right: ScreenUtil().setSp(10)),
          child: Row(
            children: [
              Container(
                  width: ScreenUtil().setSp(170),
                  height: 39,
                  child: _getReturnButton()),
              SizedBox(width: 15),
              Container(
                  width: ScreenUtil().setSp(170),
                  height: 39,
                  child: _getInvoiceButton()),
            ],
          ),
        );
      }
    }
  }

  _getOrderStatusImage() {
    String icon;
    if (c.orderDetail.value.mallOrderStatus == OrderConstant.statusInit) {
      icon = R.assetsIconOrderInit;
    } else if (c.orderDetail.value.mallOrderStatus ==
        OrderConstant.statusStocking) {
      icon = R.assetsIconOrderStocking;
    } else if (c.orderDetail.value.mallOrderStatus ==
        OrderConstant.statusTransport) {
      icon = R.assetsIconOrderTransport;
    } else if (c.orderDetail.value.mallOrderStatus ==
        OrderConstant.statusFinish) {
      icon = R.assetsIconOrderFinish;
    }

    return Image.asset(icon);
  }

  _getTransferView() {
    if (c.orderDetail.value.receiptCardDTO.bankName != null) {
      return Container(
        height: 129,
        color: Colors.white,
        margin: EdgeInsets.only(
            left: 8, right: 8),
        child: Column(
          children: [
            SizedBox(height: 8),
            Row(children: [
              SizedBox(width: 14),
              Text("对公转账信息",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 14)),
              Expanded(child: SizedBox()),
              Container(
                height: 22,
                width: 63,
                child: OutlineButton(
                  child: Text("复制",
                      style: TextStyle(
                          fontSize: 12,
                          color: Colors.black54)),
                  padding: EdgeInsets.zero,
                  borderSide: BorderSide(color: Color(0xFF40A8B3)),
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(4))),
                  onPressed: () {
                    c.copyTransfer();
                  },
                ),
              ),
              SizedBox(width: 14),
            ]),
            SizedBox(height: 16),
            Row(
              children: [
                SizedBox(width: 19),
                Expanded(
                    child: Text("公司名称",
                        style: TextStyle(fontSize: 12)),
                    flex: 6),
                Expanded(
                    child: Text(
                        "${c.orderDetail.value.receiptCardDTO.companyName}",
                        style: TextStyle(fontSize: 10)),
                    flex: 18),
              ],
            ),
            Row(
              children: [
                SizedBox(width: 19),
                Expanded(
                    child: Text("银行",
                        style: TextStyle(fontSize: 12)),
                    flex: 6),
                Expanded(
                    child: Text(
                        "${c.orderDetail.value.receiptCardDTO.bankName}",
                        style: TextStyle(fontSize: 10)),
                    flex: 18),
              ],
            ),
            Row(
              children: [
                SizedBox(width: 19),
                Expanded(
                    child: Text("分行",
                        style: TextStyle(fontSize: 12)),
                    flex: 6),
                Expanded(
                    child: Text(
                        "${c.orderDetail.value.receiptCardDTO.branchName}",
                        style: TextStyle(fontSize: 10)),
                    flex: 18),
              ],
            ),
            Row(
              children: [
                SizedBox(width: 19),
                Expanded(
                    child: Text("卡号",
                        style: TextStyle(fontSize: 12)),
                    flex: 6),
                Expanded(
                    child: Text("${c.orderDetail.value.receiptCardDTO.cardNo}",
                        style: TextStyle(fontSize: 10)),
                    flex: 18),
              ],
            )
          ],
        ),
      );
    }
    return Container();
  }

  _getReturnButton() {
    if (!c.orderDetail.value.hasReturn && c.orderDetail.value.hasInvoiced) {
      return Container();
    }
    if (!c.orderDetail.value.hasReturn) {
      return OutlineButton(
        child: Text("申请退货"),
        padding: EdgeInsets.zero,
        color: Colors.white,
        textColor: Colors.red,
        borderSide: BorderSide(color: Colors.red),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.orderReturn();
        },
      );
    } else if (c.orderDetail.value.hasReturn &&
        c.orderDetail.value.returnPriceTotal == 0) {
      return OutlineButton(
        child: Text("申请退货中"),
        padding: EdgeInsets.zero,
        color: Colors.white,
        textColor: Colors.red,
        borderSide: BorderSide(color: Colors.red),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.jumpToReturn();
        },
      );
    } else if (c.orderDetail.value.hasReturn &&
        c.orderDetail.value.returnPriceTotal > 0) {
      return OutlineButton(
        child: Text("查看退货详情"),
        padding: EdgeInsets.zero,
        color: Colors.white,
        textColor: Colors.red,
        borderSide: BorderSide(color: Colors.red),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.returnDetail();
        },
      );
    }
  }

  _getInvoiceButton() {
    if ((!c.orderDetail.value.hasInvoiced && !c.orderDetail.value.hasReturn) ||
        (!c.orderDetail.value.hasInvoiced &&
            c.orderDetail.value.hasReturn &&
            c.orderDetail.value.returnPriceTotal > 0)) {
      return FlatButton(
        child: Text("申请开票"),
        padding: EdgeInsets.zero,
        color: Color(0xFF40A8B3),
        textColor: Colors.white,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.orderInvoice();
        },
      );
    } else if (!c.orderDetail.value.hasInvoiced &&
        c.orderDetail.value.hasReturn &&
        c.orderDetail.value.returnPriceTotal == 0) {
      return FlatButton(

        child: Text("申请开票"),
        padding: EdgeInsets.zero,
        color: Color(0xFFD8D8D8),
        textColor: Colors.white,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          return null;
        },
      );
    } else if (c.orderDetail.value.hasInvoiced &&
        c.orderDetail.value.hasReturn) {
      return FlatButton(
        child: Text("查看发票"),
        padding: EdgeInsets.zero,
        color: Color(0xFF40A8B3),
        textColor: Colors.white,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.jumpToInvoice();
        },
      );
    } else if (c.orderDetail.value.hasInvoiced &&
        c.orderDetail.value.hasReturn) {
      return FlatButton(
        child: Text("查看发票"),
        padding: EdgeInsets.zero,
        color: Color(0xFF40A8B3),
        textColor: Colors.white,
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(4))),
        onPressed: () {
          c.jumpToInvoice();
        },
      );
    }
  }
}
